﻿using System;
using System.Linq;
using Csla;
using Csla.Data;
using Csla.Rules;
using Csla.Serialization;
using Csla.Rules.CommonRules;
using Validation;
using Authorization;


namespace HeThongThongTin.Library
{
    [Serializable()]
    public class DM_CapNganh : BusinessBase<DM_CapNganh>
    {
        #region Validation messages
        private static string DM_CapNganh_Ma_NotNull_ValidationMessage = Language.LanguageService.GetValidationMessage("DM_CapNganh_Ma_NotNull_ValidationMessage");
        private static string DM_CapNganh_Ma_Max_ValidationMessage = Language.LanguageService.GetValidationMessage("DM_CapNganh_Ma_Max_ValidationMessage");
        private static string DM_CapNganh_Ma_Unique_ValidationMessage = Language.LanguageService.GetValidationMessage("DM_CapNganh_Ma_Unique_ValidationMessage");
        private static string DM_CapNganh_Ten_NotNull_ValidationMessage = Language.LanguageService.GetValidationMessage("DM_CapNganh_Ten_NotNull_ValidationMessage");
        #endregion

        #region Authorization messages

        #endregion

        #region Business methods
        private static PropertyInfo<int> KhoaProperty = RegisterProperty<int>(p => p.Khoa);
        public int Khoa
        {
            get { return GetProperty(KhoaProperty); }
            private set { SetProperty(KhoaProperty, value); }
        }

        private static PropertyInfo<string> MaProperty = RegisterProperty<string>(p => p.Ma, "Ma");
        public string Ma
        {
            get { return GetProperty(MaProperty); }
            set { SetProperty(MaProperty, value); }
        }

        private static PropertyInfo<string> TenProperty = RegisterProperty<string>(p => p.Ten, "Ten");
        public string Ten
        {
            get { return GetProperty(TenProperty); }
            set { SetProperty(TenProperty, value); }
        }

        private static PropertyInfo<string> GhiChuProperty = RegisterProperty<string>(p => p.GhiChu, "GhiChu");
        public string GhiChu
        {
            get { return GetProperty(GhiChuProperty); }
            set { SetProperty(GhiChuProperty, value); }
        }

        private static PropertyInfo<string> ErrorMessageProperty = RegisterProperty<string>(p => p.ErrorMessage, "ErrorMessage");
        public string ErrorMessage
        {
            get { return GetProperty(ErrorMessageProperty); }
            set { SetProperty(ErrorMessageProperty, value); }
        }

        #endregion

        #region Custom Methods
        //Todo: add custom methods here
        #endregion

        #region Business Rules
        protected override void AddBusinessRules()
        {
            base.AddBusinessRules();

            //Authorization rules
            //BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.ReadProperty, KhoaProperty, "DM_CapNganh.Khoa.Read"));
            //BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.WriteProperty, KhoaProperty, "DM_CapNganh.Khoa.Write"));
            //BusinessRules.AddRule(new CanRead(KhoaProperty, -2, CanRead_AuthorizationMessage));
            //BusinessRules.AddRule(new CanWrite(KhoaProperty, -2, CanWrite_AuthorizationMessage));
            //BusinessRules.AddRule(new StopProcessing(KhoaProperty, -1, StopProcessing_AuthorizationMessage));

            //BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.ReadProperty, MaProperty, "DM_CapNganh.Ma.Read"));
            //BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.WriteProperty, MaProperty, "DM_CapNganh.Ma.Write"));
            //BusinessRules.AddRule(new CanRead(MaProperty, -2, CanRead_AuthorizationMessage));
            //BusinessRules.AddRule(new CanWrite(MaProperty, -2, CanWrite_AuthorizationMessage));
            //BusinessRules.AddRule(new StopProcessing(MaProperty, -1, StopProcessing_AuthorizationMessage));

            //BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.ReadProperty, TenVietProperty, "DM_CapNganh.TenViet.Read"));
            //BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.WriteProperty, TenVietProperty, "DM_CapNganh.TenViet.Write"));
            //BusinessRules.AddRule(new CanRead(TenVietProperty, -2, CanRead_AuthorizationMessage));
            //BusinessRules.AddRule(new CanWrite(TenVietProperty, -2, CanWrite_AuthorizationMessage));
            //BusinessRules.AddRule(new StopProcessing(TenVietProperty, -1, StopProcessing_AuthorizationMessage));

            //BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.ReadProperty, TenAnhProperty, "DM_CapNganh.TenAnh.Read"));
            //BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.WriteProperty, TenAnhProperty, "DM_CapNganh.TenAnh.Write"));
            //BusinessRules.AddRule(new CanRead(TenAnhProperty, -2, CanRead_AuthorizationMessage));
            //BusinessRules.AddRule(new CanWrite(TenAnhProperty, -2, CanWrite_AuthorizationMessage));
            //BusinessRules.AddRule(new StopProcessing(TenAnhProperty, -1, StopProcessing_AuthorizationMessage));

            //BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.ReadProperty, DienGiaiProperty, "DM_CapNganh.DienGiai.Read"));
            //BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.WriteProperty, DienGiaiProperty, "DM_CapNganh.DienGiai.Write"));
            //BusinessRules.AddRule(new CanRead(DienGiaiProperty, -2, CanRead_AuthorizationMessage));
            //BusinessRules.AddRule(new CanWrite(DienGiaiProperty, -2, CanWrite_AuthorizationMessage));
            //BusinessRules.AddRule(new StopProcessing(DienGiaiProperty, -1, StopProcessing_AuthorizationMessage));

            //BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.ReadProperty, KichHoatProperty, "DM_CapNganh.KichHoat.Read"));
            //BusinessRules.AddRule(new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.WriteProperty, KichHoatProperty, "DM_CapNganh.KichHoat.Write"));
            //BusinessRules.AddRule(new CanRead(KichHoatProperty, -2, CanRead_AuthorizationMessage));
            //BusinessRules.AddRule(new CanWrite(KichHoatProperty, -2, CanWrite_AuthorizationMessage));
            //BusinessRules.AddRule(new StopProcessing(KichHoatProperty, -1, StopProcessing_AuthorizationMessage));

            //Validation rules

            BusinessRules.AddRule(new StringRequiredValidation(MaProperty, DM_CapNganh_Ma_NotNull_ValidationMessage));

            BusinessRules.AddRule(new StringMaxValidation(MaProperty, DM_CapNganh_Ma_Max_ValidationMessage, 20));

            BusinessRules.AddRule(new UniqueValidation(MaProperty, DM_CapNganh_Ma_Unique_ValidationMessage, "DM_CapNganh", "Ma", "string"));

            BusinessRules.AddRule(new StringRequiredValidation(TenProperty, DM_CapNganh_Ten_NotNull_ValidationMessage));


        }
        #endregion

        #region Object Authorization Rules
        //private static void AddObjectAuthorizationRules()
        //{
        //    Csla.Rules.BusinessRules.AddRule(typeof(DM_CapNganh), new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.CreateObject, "DM_CapNganh.Create()"));
        //    Csla.Rules.BusinessRules.AddRule(typeof(DM_CapNganh), new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.GetObject, "DM_CapNganh.Get()"));
        //    Csla.Rules.BusinessRules.AddRule(typeof(DM_CapNganh), new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.EditObject, "DM_CapNganh.Edit()"));
        //    Csla.Rules.BusinessRules.AddRule(typeof(DM_CapNganh), new Csla.Rules.CommonRules.IsInRole(Csla.Rules.AuthorizationActions.DeleteObject, "DM_CapNganh.Delete()"));
        //}
        #endregion

        #region Factory Methods
        public static void NewDM_CapNganh(EventHandler<DataPortalResult<DM_CapNganh>> callback)
        {
            var dp = new DataPortal<DM_CapNganh>();
            dp.CreateCompleted += callback;
            dp.BeginCreate();
        }

        public static void DeleteDM_CapNganh(int id, EventHandler<DataPortalResult<DM_CapNganh>> callback)
        {
            var dp = new DataPortal<DM_CapNganh>();
            dp.DeleteCompleted += callback;
            dp.BeginDelete(new SingleCriteria<DM_CapNganh, int>(id), callback);
        }

        public static void GetDM_CapNganh(int id, EventHandler<DataPortalResult<DM_CapNganh>> callback)
        {
            var dp = new DataPortal<DM_CapNganh>();
            dp.FetchCompleted += callback;
            dp.BeginFetch(new SingleCriteria<DM_CapNganh, int>(id), callback);
        }

#if !SILVERLIGHT
        public static DM_CapNganh NewDM_CapNganh()
        {
            return DataPortal.Create<DM_CapNganh>();
        }

        public static void DeleteDM_CapNganh(int id)
        {
            DataPortal.Delete<DM_CapNganh>(new SingleCriteria<DM_CapNganh, int>(id));
        }

        public static DM_CapNganh GetDM_CapNganh(int id)
        {
            return DataPortal.Fetch<DM_CapNganh>(new SingleCriteria<DM_CapNganh, int>(id));
        }

        private DM_CapNganh()
        { /* require use of factory methods */ }
#else
        public DM_CapNganh()
        { /* require by MobileFormatter */ }
#endif
        #endregion

        #region Root object data access
#if !SILVERLIGHT
        protected override void DataPortal_Create()
        {
            using (BypassPropertyChecks)
            {
                base.DataPortal_Create();

                //Todo: Load default value here

            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SingleCriteria<DM_CapNganh, int> criteria)
        {
            try
            {
                using (var ctx =
                     ContextManager<HeThongThongTin.DataAccess.HeThongThongTinDataContext>.
                     GetManager(HeThongThongTin.DataAccess.Database.HeThongThongTin))
                {
                    //get DM_CapNganh data
                    var data = (from p in ctx.DataContext.DM_CapNganhs
                                where p.Khoa == criteria.Value
                                select p).Single();

                    using (BypassPropertyChecks)
                    {
                        LoadProperty(KhoaProperty, data.Khoa);
                        LoadProperty(MaProperty, data.Ma);
                        LoadProperty(TenProperty, data.Ten);
                        LoadProperty(GhiChuProperty, data.GhiChu);
                    }
                }
                LoadProperty(ErrorMessageProperty, "Load Successfully");
            }
            catch (Exception ex)
            {
                LoadProperty(ErrorMessageProperty, ex.Message);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            try
            {
                using (var ctx =
                     ContextManager<HeThongThongTin.DataAccess.HeThongThongTinDataContext>.
                     GetManager(HeThongThongTin.DataAccess.Database.HeThongThongTin))
                {
                    using (BypassPropertyChecks)
                    {
                        //insert DM_CapNganh data
                        HeThongThongTin.DataAccess.DM_CapNganh data = new HeThongThongTin.DataAccess.DM_CapNganh
                        {
                            Khoa = this.Khoa,
                            Ma = this.Ma,
                            Ten = this.Ten,
                            GhiChu = this.GhiChu
                            //ToDo: apply trace user action
                            //NguoiTao = ApplicationContext.User.Identity.Id;
                        };

                        ctx.DataContext.DM_CapNganhs.InsertOnSubmit(data);
                        ctx.DataContext.SubmitChanges();

                        //reload indentity
                        LoadProperty(KhoaProperty, data.Khoa);

                        //update child objects
                        FieldManager.UpdateChildren(this);
                    }
                }
                LoadProperty(ErrorMessageProperty, "Thêm mới thành công");
            }
            catch (Exception ex)
            {
                LoadProperty(ErrorMessageProperty, ex.Message);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            try
            {
                using (var ctx =
                   ContextManager<HeThongThongTin.DataAccess.HeThongThongTinDataContext>.
                   GetManager(HeThongThongTin.DataAccess.Database.HeThongThongTin))
                {
                    using (BypassPropertyChecks)
                    {
                        //update DM_CapNganh data
                        var data = (from p in ctx.DataContext.DM_CapNganhs
                                    where p.Khoa == this.Khoa
                                    select p).Single();

                        data.Khoa = this.Khoa;
                        data.Ma = this.Ma;
                        data.Ten = this.Ten;
                        data.GhiChu = this.GhiChu;

                        //ToDo: apply trace user action
                        //data.NguoiSua = ApplicationContext.User.Identity.Id;

                        ctx.DataContext.SubmitChanges();

                        //update child objects
                        FieldManager.UpdateChildren(this);
                    }

                }
                LoadProperty(ErrorMessageProperty, "Cập nhật thành công");
            }
            catch (Exception ex)
            {
                LoadProperty(ErrorMessageProperty, ex.Message);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            try
            {
                DataPortal_Delete(new SingleCriteria<DM_CapNganh, int>(Khoa));
            }
            catch (Exception ex)
            {
                LoadProperty(ErrorMessageProperty, ex.Message);
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(SingleCriteria<DM_CapNganh, int> criteria)
        {
            try
            {
                using (var ctx =
                   ContextManager<HeThongThongTin.DataAccess.HeThongThongTinDataContext>.
                   GetManager(HeThongThongTin.DataAccess.Database.HeThongThongTin))
                {
                    //delete DM_CapNganh data
                    var data = (from p in ctx.DataContext.DM_CapNganhs
                                where p.Khoa == criteria.Value
                                select p).Single();

                    ctx.DataContext.DM_CapNganhs.DeleteOnSubmit(data);
                    //ToDo: apply trace user action
                    //data.NguoiXoa = ApplicationContext.User.Identity.Id;

                    ctx.DataContext.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                LoadProperty(ErrorMessageProperty, ex.Message);
            }
        }
#endif
        #endregion
    }
}

